<template>
  <!--table表格组件-->
  <div class="def-table">
    <!--table表格-->
    <!-- v-loading="isLoading" -->
    <el-table :data="tableData" :list="tableColumnList" v-bind="$attrs" @click.stop>
      <el-table-column label="#" type="index" width="60"></el-table-column>
      <template v-for="(item, index) in tableColumnList" :key="item.prop">
        <!--正常栏-->
        <template v-if="item.prop !== 'action' && !item.tableColumn?.hidden">
          <el-table-column
            :prop="item.prop"
            :label="item.label"
            align="center"
            v-bind="item?.tableColumn">
            <template #default="scope">
              <!--前置图标-->
              <def-icon v-if="item.icon" :icon="item.icon" :style="item.iconStyle" />
              <slot :name="item.prop" :row="scope.row" :$index="scope.$index" :prop="item.prop">
                <span>{{ scope.row[item.prop] }}</span>
              </slot>
            </template>
          </el-table-column>
        </template>
        <!--操作栏-->
        <template
          v-if="
            item.prop === 'action' &&
            index === tableColumnList.length - 1 &&
            !item.tableColumn?.hidden
          ">
          <el-table-column
            :prop="item.prop"
            :label="item.label"
            align="right"
            width="200"
            v-bind="item?.tableColumn">
            <!--操作栏表头-->
            <template #header>
              <slot name="header">
                <el-button
                  :size="$attrs.size"
                  type="primary"
                  v-bind="item.actionAttrs"
                  @click="handleRowAdd">
                  <def-icon icon="CirclePlus" />
                  <span>新增</span>
                </el-button>
              </slot>
            </template>
            <!--操作栏按钮-->
            <template #default="scope">
              <slot name="action" :scope="scope">
                <el-button
                  :size="$attrs.size"
                  type="success"
                  v-bind="item.actionAttrs"
                  @click="handleRowEdit(scope)">
                  <def-icon icon="edit" />
                  <span>编辑</span>
                </el-button>
                <el-button
                  :size="$attrs.size"
                  type="danger"
                  v-bind="item.actionAttrs"
                  @click="handleRowDelete(scope)">
                  <def-icon icon="delete" />
                  <span>删除</span>
                </el-button>
              </slot>
            </template>
          </el-table-column>
        </template>
      </template>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { inject, PropType, ref, watch } from 'vue'
import { ITableColumn } from './types'
import { ElMessage, ElMessageBox } from 'element-plus'

/* 参数声明 */
const props = defineProps({
  list: {
    type: Array as PropType<ITableColumn[]>,
    required: true
  }, // 表格项配置数组
  data: {
    type: Array,
    required: true
  } // 表格数据
})
const emits = defineEmits(['rowAdd', 'rowEdit', 'rowDelete'])
// const isLoading = inject('isLoading')

/* 数据处理 */
const tableData = ref<any[]>(props.data)
const tableColumnList = ref<ITableColumn[]>(props.list)
// 表格校验
const verifyTable = () => {
  if (!props.list?.length) throw new Error('缺少表格项配置')
  if (!props.data) throw new Error('缺少表格数据')
}
verifyTable()
// 监听最新表格数据变化
watch(
  () => props.data,
  (val) => {
    tableData.value = val
  }
)

// 行操作处理
const handleRowAdd = () => {
  console.log('table 行新增')
  emits('rowAdd')
}
const handleRowEdit = (scope: any) => {
  console.log('table 行编辑', scope.row)
  emits('rowEdit', scope.row)
}
const handleRowDelete = (scope: any) => {
  console.log('table 行删除', scope.row)
  ElMessageBox.confirm('是否删除该行数据？', '删除提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 触发外层事件
    emits('rowDelete', scope.row)
    ElMessage.success('删除成功')
  })
}
</script>

<style scoped lang="scss">
.def-table {
  flex: 1;
  @include clearElBtnFocus;
  .def-icon {
    margin-right: 5px;
  }
  .icon-edit {
    margin-right: 0;
    margin-left: 10px;
    font-size: 1.2em;
    &:hover {
      opacity: 0.7;
      cursor: pointer;
    }
  }
  .cell-edit {
    @include flexRow;
    .cell-edit-icons {
      margin-left: 15px;
      width: 80px;
      font-size: 1.5em;

      @include flexRow;
      .icon-confirm,
      .icon-close {
        flex: 1;
        color: green;
        &:hover {
          opacity: 0.7;
          cursor: pointer;
        }
      }
      .icon-close {
        margin-right: 0;
        color: red;
      }
    }
  }
}
</style>
